package com.luomo.java.juc.blockqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author LiuMei
 * @date 2021-04-12.
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        queue.offer("a",2L, TimeUnit.SECONDS);
        queue.offer("a", 2L, TimeUnit.SECONDS);
        queue.offer("a",2L, TimeUnit.SECONDS);
        queue.offer("a",2L, TimeUnit.SECONDS);

        queue.poll(1,TimeUnit.SECONDS);
        queue.poll(1,TimeUnit.SECONDS);
        queue.poll(1,TimeUnit.SECONDS);
        queue.poll(1,TimeUnit.SECONDS);
    }

    private static void block(BlockingQueue<String> queue) throws InterruptedException {
        queue.put("a");
        queue.put("b");
        queue.put("c");
        //一直等待，等到成功put为止
//        queue.put("d");
        queue.take();
        queue.take();
        queue.take();
        //同样一直等待，直到take成功为止
//        queue.take();
    }

    private static void noException(BlockingQueue<String> queue) {
        System.out.println(queue.peek());

        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("b"));
        System.out.println(queue.offer("c"));
        System.out.println(queue.offer("d"));

        System.out.println(queue.peek());

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }

    private static void exception(BlockingQueue<String> queue) {
        System.out.println(queue.add("a"));
        System.out.println(queue.add("b"));
        System.out.println(queue.add("c"));
//        System.out.println(queue.add("d"));

        System.out.println(queue.element());

        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
//        System.out.println(queue.remove());
    }

}
